#include "loginwidget.h"
#include "ui_loginwidget.h"
#include <QPixmap>
#include <QDialog>
#include "logindialog.h"
#include "itemwidget.h"
#include "ctooltip.h"
#include "chatwindow.h"

#include <QListWidgetItem>

int LoginWidget::ID = 0;
CToolTip *g_toolTip;

LoginWidget::LoginWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LoginWidget)
{
    ui->setupUi(this);

    database = QSqlDatabase::addDatabase("QSQLITE");
    database.setDatabaseName("database.db");

    QSqlQuery sql_query;        //改变量必须在成功打开数据库后定义才有效

    //打开数据库
    if(!database.open())
    {
        qDebug()<<database.lastError();
        qFatal("failed to connect.") ;
    }
    else
    {

        QSqlQuery query;        //变量必须在成功打开数据库后定义才有效
        QString execstring="select * from user";

        ctoolTip = new CToolTip();
        // 定义全局的ToolTip，方便使用
        g_toolTip = ctoolTip;

        // 本行代码主要针对ListWidgetItem右键点击时才生效的
        ui->listWidget->setMouseTracking(true);

        if(!query.exec(execstring))
        {
            qDebug()<<QString("chatid");
            qDebug()<<query.lastError();
    //        matchFlag=false;
        }
        else
        {
            // 添加测试数据
            while (query.next()) {
                usr_id = query.value(0).toInt();
                usr_passwd = query.value(1).toString();
                usr_name = query.value(2).toString();
                usr_email = query.value(3).toString();
                usr_history = query.value(4).toInt();

                qDebug()<<QString("chatid=%1    passwd=%2     name=%3       email=%4    history=%5").arg(usr_id).arg(usr_passwd).arg(usr_name).arg(usr_email).arg(usr_history);

                ItemWidget *itemWidget = new ItemWidget(ui->listWidget);
                itemWidget->setText(QPixmap(QString(":/images/face%1").arg(usr_id)).scaled(80, 80),
                                            QString("%1").arg(usr_name), QString("127.0.0.1:800%1").arg(usr_id));
                QListWidgetItem *listItem = new QListWidgetItem(ui->listWidget);
                // 此处的size如果不设置，界面被压缩了看不出ItemWidget的效果，高度一定要设置
                listItem->setSizeHint(QSize(200, 70));
                ui->listWidget->setItemWidget(listItem, itemWidget);


            }
        }
    }
    QObject::connect(ui->listWidget,SIGNAL(itemClicked(QListWidgetItem*)),this,SLOT(conChat(QListWidgetItem*)));
    qDebug()<<"LoginWidget:"+LoginDialog::NICKNAME;
    ui->nickname->setText(LoginDialog::NICKNAME);
    QString tempstring="select * from user where name='"+LoginDialog::NICKNAME+"'";
    qDebug()<<tempstring;
    if(!sql_query.exec(tempstring))
    {
        qDebug()<<sql_query.lastError();
        matchFlag=false;
    }
    else
    {
        while(sql_query.next())
        {
            usr_id = sql_query.value(0).toInt();
            usr_passwd = sql_query.value(1).toString();
            usr_name = sql_query.value(2).toString();
            usr_email = sql_query.value(3).toString();
            usr_history = sql_query.value(4).toInt();

            qDebug()<<QString("chatid=%1    passwd=%2     name=%3       email=%4    history=%5").arg(usr_id).arg(usr_passwd).arg(usr_name).arg(usr_email).arg(usr_history);

        }
        if(usr_name==LoginDialog::NICKNAME)  matchFlag=true;
        else                matchFlag=false;
    }

    qDebug()<<matchFlag;
    if(matchFlag==true)
    {
        QString path=":/images/facex.jpg";
        QString diff="face"+QString::number(usr_id);
        path.replace("facex",diff);
        qDebug()<<path;

        QImage img;
        img.load(path);
        QPixmap pic=QPixmap::fromImage(img.scaled(ui->userPic->width(),ui->userPic->height()));
        ui->userPic->setPixmap(pic);
        ui->ipaddress->setText(QString("127.0.0.1:800%1").arg(usr_id));
    }
    else
    {

        QPixmap pic;
        ui->userPic->setPixmap(pic);
    }
}

LoginWidget::~LoginWidget()
{
    delete ui;
}

void LoginWidget::on_quit_clicked()
{
    close();
    LoginDialog dlg;
    if(dlg.exec()==QDialog::Accepted) {
        show();
    }
}

void LoginWidget::conChat(QListWidgetItem*)
{
    qDebug()<<QString("onChat:%1").arg(usr_id);
    LoginWidget::ID = ui->listWidget->currentRow()+1;
    chatWindow=new ChatWindow;
    chatWindow->show();
}
